home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / NeXTGo / Source / count.c < prev    next >
C/C++ Source or Header  |  1993-02-08  |  1KB  |  64 lines

  1. #include "comment.header"
  2.  
  3. #define EMPTY 0
  4.  
  5. extern unsigned char p[19][19], ml[19][19];
  6. extern int lib, MAXX, MAXY;
  7.  
  8. void count(int i, int j, int color)
  9.      /* count liberty of color piece at i, j */
  10. {
  11.   /* set current piece as marked */
  12.   ml[i][j] = EMPTY;
  13.   
  14.   /* check North neighbor */
  15.   if (i != EMPTY)
  16.     {
  17.       if ((p[i - 1][j] == EMPTY) && ml[i - 1][j])
  18.     {
  19.       ++lib;
  20.       ml[i - 1][j] = EMPTY;
  21.     }
  22.       else
  23.     if ((p[i - 1][j] == color) && ml[i - 1][j])
  24.       count(i - 1, j, color);
  25.     }
  26.   /* check South neighbor */
  27.   if (i != MAXX - 1)
  28.     {
  29.       if ((p[i + 1][j] == EMPTY) && ml[i + 1][j])
  30.     {
  31.       ++lib;
  32.       ml[i + 1][j] = EMPTY;
  33.     }
  34.       else
  35.     if ((p[i + 1][j] == color) && ml[i + 1][j])
  36.       count(i + 1, j, color);
  37.     }
  38.   /* check West neighbor */
  39.   if (j != EMPTY)
  40.     {
  41.       if ((p[i][j - 1] == EMPTY) && ml[i][j - 1])
  42.     {
  43.       ++lib;
  44.       ml[i][j - 1] = EMPTY;
  45.     }
  46.       else
  47.     if ((p[i][j - 1] == color) && ml[i][j - 1])
  48.       count(i, j - 1, color);
  49.     }
  50.   /* check East neighbor */
  51.   if (j != MAXY - 1)
  52.     {
  53.       if ((p[i][j + 1] == EMPTY) && ml[i][j + 1])
  54.     {
  55.       ++lib;
  56.       ml[i][j + 1] = EMPTY;
  57.     }
  58.       else
  59.     if ((p[i][j + 1] == color) && ml[i][j + 1])
  60.       count(i, j + 1, color);
  61.     }
  62. }  /* end count */
  63.  
  64.